package com.neu.service.impl;

import com.google.common.collect.Lists;
import com.neu.constant.ProjectTypeEnum;
import com.neu.dto.ProjectDataDTO;
import com.neu.dto.ProvinceDataDTO;
import com.neu.mapper.ArticleMapper;
import com.neu.mapper.EventMapper;
import com.neu.mapper.ProjectMapper;
import com.neu.mapper.ProvinceMapper;
import com.neu.pojo.Article;
import com.neu.pojo.Event;
import com.neu.pojo.Province;
import com.neu.service.ProvinceService;
import com.neu.vo.DashboardVO;
import com.neu.vo.ProvinceVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Objects;

@Slf4j
@Service
public class ProvinceServiceImpl implements ProvinceService {

    @Resource
    private ProjectMapper projectMapper;

    @Resource
    private ProvinceMapper provinceMapper;

    @Resource
    private ArticleMapper articleMapper;

    @Resource
    private EventMapper eventMapper;

    private final int ARTICLE_INIT_VALUE = 0;

    @Override
    public DashboardVO dashboard() {
        log.info("首页信息查询开始...");
        DashboardVO dashboardVO = new DashboardVO();
        Double we = projectMapper.queryCapByType(ProjectTypeEnum.WE.type);
        Double photovoltaic = projectMapper.queryCapByType(ProjectTypeEnum.PHOTOVOLTAIC.type);
        Double cap = projectMapper.queryTotalCap();
        dashboardVO.setWe(Objects.isNull(we)?0:we);
        dashboardVO.setPhotovoltaic(Objects.isNull(photovoltaic)?0:photovoltaic);
        dashboardVO.setCap(Objects.isNull(cap)?0:cap);
//        DashboardVO dashboardVO = projectMapper.queryDashboardData();
//        if(Objects.isNull(dashboardVO)) dashboardVO=new DashboardVO();
        List<ProvinceDataDTO> provinceDataDTOList = projectMapper.queryProvinceDataList();
        List<ProjectDataDTO> projectDataList = projectMapper.queryAllProjectDataList();
        dashboardVO.setProvinces(Objects.isNull(provinceDataDTOList)? Lists.newArrayList():provinceDataDTOList);
        dashboardVO.setProjects(Objects.isNull(projectDataList)? Lists.newArrayList():projectDataList);
        // 查询公司事迹
        List<Event> events = eventMapper.listEvent();
        dashboardVO.setEvents(Objects.isNull(events)? Lists.newArrayList():events);
        log.info("首页信息查询结束...");
        return dashboardVO;
    }

    @Override
    public ProvinceVO queryProvince(int id,String name) {
        Province province = provinceMapper.queryProvince(id);
        if(Objects.isNull(province)){
            log.info("初始化省份: id:{},name:{}",id,name);
            Article res = Article.builder().title(name+"-资源分布").content("").build();
            articleMapper.insertArticle(res);
            Article mar = Article.builder().title(name+"-市场简介").content("").build();
            articleMapper.insertArticle(mar);
            province = Province.builder().id(id).name(name).resourceDistribution(res.getId()).marketIntroduction(mar.getId()).expansion("").build();
            provinceMapper.insertProvince(province);
        }
        List<ProjectDataDTO> projectDataDTOList = projectMapper.queryProjectDataList(id);

        Double we = projectMapper.queryProvinceCapByType(id,ProjectTypeEnum.WE.type);
        Double photovoltaic = projectMapper.queryProvinceCapByType(id,ProjectTypeEnum.PHOTOVOLTAIC.type);
        Double cap = projectMapper.queryProvinceTotalCap(id);

        ProvinceVO provinceVO = ProvinceVO.builder()
                .id(province.getId())
                .name(province.getName())
                .we(Objects.isNull(we)?0:we)
                .photovoltaic(Objects.isNull(photovoltaic)?0:photovoltaic)
                .cap(Objects.isNull(cap)?0:cap)
                .resourceDistribution(province.getResourceDistribution())
                .marketIntroduction(province.getMarketIntroduction())
                .projects(projectDataDTOList).build();
        return provinceVO;
    }

}
